package com.jk.libsm.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getNowDate() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(calendar.getTime());
        return date;
    }

    /**
     * 对给定时间增加天数
     *
     * @param day     要增加的天数
     * @param newDate 给定的时间
     * @return String类型的增加后的时间
     * @throws ParseException
     */
    public static String plusDay(long day, String newDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currdate = new Date();
        try {
            currdate = format.parse(newDate);
        }catch (ParseException e){
            e.printStackTrace();
        }
        long time = currdate.getTime();
        day = day * 24 * 60 * 60 * 1000;
        time += day;
        Date end = new Date(time);
        String enddate = format.format(end);
        return enddate;
    }

    /**
     * 指定时间减去当前时间，计算剩余天数
     *
     * @param newDate
     * @return
     * @throws ParseException
     */
    public static String releaseDay(String newDate1,String newDate2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = new Date() ;
        Date d2 = new Date() ;
        try{
            d1 = df.parse(newDate1);
            d2 = df.parse(newDate2);
        }catch (ParseException e){
            e.printStackTrace();
        }
        long diff = d1.getTime() - d2.getTime();//这样得到的差值是毫秒级别
        long days = diff / (1000 * 60 * 60 * 24);
        return days + "";
    }

}
